package com.hntxk.cqzd.diacrisis.action;/**
 * @ClassName: ${user}
 * @Author:liuqing
 * @Description
 * @Date: Created in 10:22 2020/5/21
 * @Modified By:
 */

import com.hntxk.core.framework.model.ResponseModel;
import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.diacrisis.entity.SelBySisIdEntity;
import com.hntxk.cqzd.diacrisis.entity.TblDiacrisisInfo;
import com.hntxk.cqzd.diacrisis.service.DiagnosticProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Comparator;
import java.util.List;

/**
 * @ClassName DiagnosticProcessController
 * @description 诊断信息业务展示控制层
 * @Author Liuqing
 * @Date 2020/5/21 10:22
 * @Version 1.0
 */
@Api(value="诊断信息流程展示", tags={"[cqzd]主页"})
@Controller
@RequestMapping("cqzd/do/process")
@Slf4j
public class DiagnosticProcessController extends CqzdBaseController {

    private static String MAPPER_NAMESPACE="com.hntxk.cqzd.diacrisis.mapper.DiagnosticProcessMapper.";

    private DiagnosticProcessService service;

    public DiagnosticProcessController(@Qualifier("processService") DiagnosticProcessService processService) {
        super(processService, LogManager.getLogger(DiagnosticProcessController.class));
        this.service = processService;
    }

    /**
     * 查询
     * @param model TblInspectLabCvmModel
     * @return json
     */
    @ApiImplicitParam(name = "queryWhere",value = "查询条件，根据卡号和身份证号")
    @ApiOperation(value = "首页查询")
    @RequestMapping(value = "/list",method = {RequestMethod.GET},produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseModel listBypatientId(@ModelAttribute TblDiacrisisInfo model) {
        model.setPagination(request);
        ResponseModel responseModel = service.query2Json(MAPPER_NAMESPACE + "list", model, false);
        List<TblDiacrisisInfo> data = (List<TblDiacrisisInfo>) responseModel.getData();
        data.forEach(p -> {
            List<SelBySisIdEntity> entitys = service.selBySisId(p.getDiacrisisId());
            if (! CollectionUtils.isEmpty(entitys)) {
                entitys.forEach(entity -> {
                    if (StringUtil.isNotNull(entity.getFruitTypeName()) && StringUtil.isNotNull(entity.getFruitExplain())) {
                        switch (entity.getFruitTypeName()) {
                            case "核型分析":
                                entity.setSortVal(5);
                                break;
                            case "FISH":
                                entity.setSortVal(4);
                                break;
                            case "CNV":
                                entity.setSortVal(3);
                                break;
                            default:
                                if (null == p.getDiagnosisStatus()){
                                    if (null != p.getInspectionTime()) {
                                        entity.setSortVal(2);
                                    }else {
                                        entity.setSortVal(0);
                                    }
                                }
                                break;
                        }

                    }else {
                        entity.setSortVal(0);
                    }
                });

                entitys.sort(Comparator.comparing(SelBySisIdEntity::getSortVal).reversed());
                p.setDiagnosisStatus(entitys.get(0).getSortVal());
            } else {
                p.setDiagnosisStatus(0);
            }

                }
        );
        responseModel.setData(data);

        return responseModel;



    }



}
